clear all;
close all;

%  =======================================================================================================================================================
%
%  Code Description:
%  This codefile generates results in section 3 and Internet Appendix D.
%
%  =======================================================================================================================================================
%
%  Major output:
%  - Figure 6: Active trading during the COVID 19-induced stress period.
%  - Figure IA.7: Flow-driven and discretionary trading during the COVID 19-induced market stress period.
%  =======================================================================================================================================================
%
%  General disclaimer:
%  This file directory produces replication code for "Connected Funds".
%  Because we cannot share the underlying data provided by the Bundesbank's Research Data and Service Centre (RDSC) and other subscription data sources,
%  we have included pseudo data to show how the raw data are formatted. 
%  Other researchers can go through a similar approval and subscription process to obtain the underlying data. (2023-04-06)
%
%  =======================================================================================================================================================

%% Set project directory

projectPath = 'C:\ConnectedFunds_Codebase\'

% Add functions folders
addpath(strcat(projectPath, 'Code\matlab_functions'));


%  ==================================================================================== Preparations ==============================================================================================

load(strcat(projectPath, 'Data\Empirical_PartI_CovidTrading'));

%% load VIX data (Source: https://www.cboe.com/tradable_products/vix/vix_historical_data/)
VIX = readtable(strcat(projectPath, 'Data\VIX\VIX_monthly.csv'));

[~,idx] = ismember(umonth,VIX.datum);
vix     = VIX.vixclose(idx);
clear idx VIX;

% Denote in billion eur
All_Spezial_Results_Discretionary   = 1000 * 10^-9 * All_Spezial_Results_Discretionary;
All_Spezial_Results_FlowDriven      = 1000 * 10^-9 * All_Spezial_Results_FlowDriven;


%  ============================================================== Generate time series barplots over Covid-subsample (start in 2020) =======================================================================


tt = find(umonth > 201912);

% Figure 6: Active trading during the COVID 19-induced stress period.

%... retail 
close
yyaxis left
h = bar(dates(tt),[All_Results_NoPriceEffect(tt,2)-All_Spezial_Results_NoPriceEffect(tt,2) ...
                   All_Results_NoPriceEffect(tt,3)-All_Spezial_Results_NoPriceEffect(tt,3) ...
                   All_Results_NoPriceEffect(tt,4)-All_Spezial_Results_NoPriceEffect(tt,4) ...
                   All_Results_NoPriceEffect(tt,5)-All_Spezial_Results_NoPriceEffect(tt,5)],'stacked');
h(1).FaceColor = 'blue';
h(2).FaceColor = 'black';
h(3).FaceColor = 'green';
h(4).FaceColor = 'cyan';
ylabel( '\Delta Holdings^c and Flows (in EUR bn)');
hold on;
plot(dates(tt),1000*10^-9*All_Spezial_TotalFlows(tt,2),'s','Color','b','MarkerFaceColor','y');
hold off;
datetick('x','mmm');
xtickangle(45);   axis tight; ylim([-30 15]);
title('Retail');  grid on;
yyaxis right
plot(dates(tt),vix(tt),'LineWidth',2); xlabel('Year: 2020'); ylabel('VIX')
legend('Corp. Bonds','Sov. Bonds','Equities','Fund shares','Flows','VIX (right)','location','SouthEast');
saveas(gcf, strcat(projectPath, 'Paper\Figures\Fig6L_Active trading during the COVID 19-induced stress period.png'),'png');

%... institutional
close
yyaxis left
h = bar(dates(tt),[All_Spezial_Results_NoPriceEffect(tt,2) ...
               All_Spezial_Results_NoPriceEffect(tt,3) ...
               All_Spezial_Results_NoPriceEffect(tt,4) ...
               All_Spezial_Results_NoPriceEffect(tt,5)],'stacked');
h(1).FaceColor = 'blue';
h(2).FaceColor = 'black';
h(3).FaceColor = 'green';
h(4).FaceColor = 'cyan';
ylabel('\Delta Holdings^c and Flows (in EUR bn)');
hold on;
plot(dates(tt),1000*10^-9*All_Spezial_TotalFlows(tt,1),'s','Color','b','MarkerFaceColor','y');
hold off;
datetick('x','mmm'); xlabel('Year: 2020');  xtickangle(45);   axis tight; ylim([-30 15]);
title('Institutional');  grid on; 
yyaxis right
plot(dates(tt),vix(tt),'LineWidth',2); ylabel('VIX')
saveas(gcf, strcat(projectPath, 'Paper\Figures\Fig6R_Active trading during the COVID 19-induced stress period.png'),'png');


% Figure IA.7: Flow-driven and discretionary trading during the COVID 19-induced market stress period.

%... retail 
close
yyaxis left
h = bar(dates(tt),[All_Spezial_Results_FlowDriven(tt,2) ...
                   All_Spezial_Results_Discretionary(tt,2)],'stacked');
h(1).FaceColor = 'blue';
h(2).FaceColor = 'black';
ylabel( 'NetPurchase (in EUR bn)');
hold off;
datetick('x','mmm'); 
xtickangle(45);   axis tight; ylim([-30 15]);
title('Retail');  grid on;
yyaxis right
plot(dates(tt),vix(tt),'LineWidth',2); xlabel('Year: 2020'); ylabel('VIX')
legend('Flow-Driven','Discretionary','VIX (right)','location','SouthEast');
saveas(gcf, strcat(projectPath, 'Paper\Figures\FigIA7L_Flow-driven and discretionary trading during the COVID 19-induced market stress period.png'),'png');

%... institutional
close
yyaxis left
h = bar(dates(tt),[All_Spezial_Results_FlowDriven(tt,1) ...
                   All_Spezial_Results_Discretionary(tt,1)],'stacked');
h(1).FaceColor = 'blue';
h(2).FaceColor = 'black';
ylabel( 'NetPurchase (in EUR bn)');
hold off;
datetick('x','mmm'); 
xtickangle(45);   axis tight; ylim([-30 15]);
title('Institutional');  grid on;
yyaxis right
plot(dates(tt),vix(tt),'LineWidth',2); xlabel('Year: 2020'); ylabel('VIX')
legend('Flow-Driven','Discretionary','VIX (right)','location','SouthEast');
saveas(gcf, strcat(projectPath, 'Paper\Figures\FigIA7R_Flow-driven and discretionary trading during the COVID 19-induced market stress period.png'),'png');